//
//  bindingViewController.m
//  随便点
//
//  Created by MWJ on 15/3/12.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "bindingViewController.h"
#import "AppDelegate.h"
#import "ggViewController.h"
#define zifu 15
#define Kgenghuang @"http://www.51anys.com:8080/waimaiwang/user/validate/original"
#define Kgenghuang1 @"http://www.51anys.com:8080/waimaiwang/user/changeValidate"
@interface bindingViewController (){
    UIButton *BUTT;
    UITextField *textfield1;
    AppDelegate *app;
    NSString *str;
    NSString *genggai;
    NSString *genggai1;
}

@end

@implementation bindingViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"zixingche"]]];
        self.navigationItem.title=@"修改绑定手机";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    str = app._phone;
    [self loading];
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loading{
    [self label];
    [self button1];
    [self text];
    [self button2];
}
-(void)label{
    UILabel *lab = [[UILabel alloc]init];
    CGFloat labX = zifu*2;
    CGFloat labY = 100;
    CGFloat labW = zifu*10;
    CGFloat labH = zifu*2;
    lab.text = str;
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:20];
    lab.frame = CGRectMake(labX, labY, labW, labH);
    lab.layer.borderColor = [[UIColor grayColor] CGColor];
    lab.layer.borderWidth = 2;
    [self.view addSubview:lab];
}
-(void)button1{
    UIButton *button =[[UIButton alloc]init];
    CGFloat buttonX = 190;
    CGFloat buttonY = 100;
    CGFloat buttonW = [UIScreen mainScreen].bounds.size.width-220;
    CGFloat buttonH = zifu*2;
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
    button.backgroundColor = [UIColor blueColor];
    [button setTitle:[NSString stringWithFormat:@"发送验证码"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
-(void)send{
    NSURL *url=[NSURL URLWithString:Kgenghuang];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"userId=%@&phone=%@",str,str];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        genggai = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:genggai waitUntilDone:YES];
        int str22 = [genggai intValue];
        if (str22 == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发送失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"发送成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }];
}
-(void)refreshData:(NSArray *)sender{
}
-(void)text{
    UITextField *textfield = [[UITextField alloc]init];
    CGFloat textfieldX = zifu*2;
    CGFloat textfieldY = 150;
    CGFloat textfieldW = [UIScreen mainScreen].bounds.size.width-60;
    CGFloat textfieldH = zifu*2;
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    textfield.secureTextEntry = YES;
    [textfield setBackgroundColor:[UIColor whiteColor]];
    textfield.font = [UIFont systemFontOfSize:15];
    textfield.frame = CGRectMake(textfieldX, textfieldY, textfieldW, textfieldH);
    textfield1 = textfield;
    [self.view addSubview:textfield];
}
-(void)button2{
    UIButton *button1 = [[UIButton alloc]init];
    CGFloat button1X = zifu*6;
    CGFloat button1Y = 200;
    CGFloat button1W = [UIScreen mainScreen].bounds.size.width-180;
    CGFloat button1H = zifu*2.5;
    button1.titleLabel.font = [UIFont systemFontOfSize:15];
    button1.frame = CGRectMake(button1X, button1Y, button1W, button1H);
    button1.backgroundColor = [UIColor greenColor];
    [button1.layer setMasksToBounds:YES];
    [button1 setTitle:[NSString stringWithFormat:@"下一步"] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [button1 addTarget:self action:@selector(ToNext) forControlEvents:UIControlEventTouchUpInside];
    BUTT = button1;
    [self.view addSubview:button1];
    
}
-(void)ToNext{
    if ([textfield1.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入验证码" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        [self right];
    }
    
}
-(void)right{
    NSURL *url=[NSURL URLWithString:Kgenghuang1];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"number=%@&phone=%@",textfield1.text,str];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        genggai1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:genggai1 waitUntilDone:YES];
        int str11 = [genggai1 intValue];
        if (str11==0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"验证码错误" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            ggViewController *g = [storyboard instantiateViewControllerWithIdentifier:@"gg"];
            [self.navigationController pushViewController:g animated:YES];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [textfield1 resignFirstResponder];
}
@end
